/**
 * 公共工具
 */
// 金额格式化(分->元 保留2位小数 并每隔3位用逗号分开 1,234.56
export const abs = (s: any, type?: number) => {
  if (/[^0-9\.]/.test(s)) return '0.00'
  if (s === null || s === 'null' || s === '') return '0.00'
  s = s.toString().replace(/^(\d*)$/, '$1.')
  s = (s + '00').replace(/(\d*\.\d\d)\d*/, '$1')
  s = s.replace('.', ',')
  let re = /(\d)(\d{3},)/
  while (re.test(s)) s = s.replace(re, '$1,$2')
  s = s.replace(/,(\d\d)$/, '.$1')
  if (type === 0) {
    let a = s.split('.')
    if (a[1] === '00') s = a[0]
  }
  return s
}
// 睡眠
export const sleep = (s: number) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(s)
    })
  })
}
// 格式化时间戳为日期
export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  const formatNumber = (n: number | string) : string => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}